
package com.lyuewei.ringtone.data; // 替换为你的数据类包名

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;


import com.elvishew.xlog.XLog;

import java.util.Calendar;
import java.util.Objects;

@Entity(tableName = "ringer_settings") // 指定表名
public class RingerSetting {

    @PrimaryKey(autoGenerate = true) // 设置为主键并自动生成
    public int id; // 主键

    private String phone; // 电话号码
    private String name;        // 名称
    private int volume;         // 音量
    private long interval;      // 间隔 (例如，响铃间隔时间，单位毫秒)
    private int times;          // 次数 (例如，响铃次数)
    private int startHour=0;
    private int endHour=23;
    private int startMinute=0;
    private int endMinute=59;
    private boolean enabled;    // 是否启用

    // 构造函数 (Room 需要一个无参或带有所有非 @Ignore 字段的构造函数)
    @Ignore
    public RingerSetting(String phone, String name, int volume, long interval, int times, boolean enabled) {
        this.phone = phone;
        this.name = name;
        this.volume = volume;
        this.interval = interval;
        this.times = times;
        this.enabled = enabled;
        this.startHour = 0;
        this.startMinute = 0;
        this.endHour = 23;
        this.endMinute = 59;
    }
    // 构造函数 (更新以包含新的时间字段)
    // 添加新设置时使用的构造函数
    public RingerSetting(String phone, String name, int volume, long interval, int times,
                         int startHour,int endHour, int startMinute,  int endMinute, boolean enabled) {
        this.phone = phone;
        this.name = name;
        this.volume = volume;
        this.interval = interval;
        this.times = times;
        this.enabled = enabled;
        this.startHour = startHour;
        this.startMinute = startMinute;
        this.endHour = endHour;
        this.endMinute = endMinute;
    }
    // Room 使用的空构造函数 (通常 Room 会自动生成，但如果你自定义了其他构造函数，最好保留一个无参构造函数)
    @Ignore
    public RingerSetting() {
        // Default constructor required by Room
    }

    /**
     * 判断当前时间是否在该设置的有效时间范围内。
     * 支持同一天内的范围 (例如 15:00 - 19:00) 和跨越午夜的范围 (例如 20:00 - 03:00)。
     *
     * @return 如果当前时间在设置的有效范围内，则返回 true，否则返回 false。
     */
    public boolean isCurrentTimeInRange() {
        Calendar calendar = Calendar.getInstance();
        int currentHour = calendar.get(Calendar.HOUR_OF_DAY); // 24小时制
        int currentMinute = calendar.get(Calendar.MINUTE);

        // 将当前时间转换为分钟数，方便比较
        int currentTimeInMinutes = currentHour * 60 + currentMinute;

        // 将开始时间和结束时间转换为分钟数
        int startTimeInMinutes = startHour * 60 + startMinute;
        int endTimeInMinutes = endHour * 60 + endMinute;
        XLog.d("时间范围：%d:%d-%d:%d", startHour, startMinute,endHour,endMinute);
        if (startTimeInMinutes <= endTimeInMinutes) {
            // 情况 1: 时间范围在同一天 (例如 15:00 - 19:00)
            return currentTimeInMinutes >= startTimeInMinutes && currentTimeInMinutes <= endTimeInMinutes;
        } else {
            // 情况 2: 时间范围跨越午夜 (例如 20:00 - 03:00)
            // 这意味着时间范围是 20:00 - 24:00 和 00:00 - 03:00 这两个时段
            return currentTimeInMinutes >= startTimeInMinutes || currentTimeInMinutes <= endTimeInMinutes;
        }
    }
    // Getter 和 Setter (Room 会使用它们访问字段)
    public int getId() { return id; }
    public void setId(int id) { this.id = id; }

    public String getPhone() { return phone; }
    public void setPhone(String phone) { this.phone = phone; }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public int getVolume() { return volume; }
    public void setVolume(int volume) { this.volume = volume; }

    public long getInterval() { return interval; }
    public void setInterval(long interval) { this.interval = interval; }

    public int getTimes() { return times; }
    public void setTimes(int times) { this.times = times; }

    public boolean isEnabled() { return enabled; }
    public void setEnabled(boolean enabled) { this.enabled = enabled; }
    public int getStartHour() {
        return startHour;
    }

    public void setStartHour(int startHour) {
        this.startHour = startHour;
    }

    public int getStartMinute() {
        return startMinute;
    }

    public void setStartMinute(int startMinute) {
        this.startMinute = startMinute;
    }

    public int getEndHour() {
        return endHour;
    }

    public void setEndHour(int endHour) {
        this.endHour = endHour;
    }

    public int getEndMinute() {
        return endMinute;
    }

    public void setEndMinute(int endMinute) {
        this.endMinute = endMinute;
    }
    // 在 RingerSetting 类中添加
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RingerSetting that = (RingerSetting) o;
        return id == that.id &&
                volume == that.volume &&
                interval == that.interval &&
                times == that.times &&
                enabled == that.enabled &&
                startHour == that.startHour && // 新增比较
                startMinute == that.startMinute && // 新增比较
                endHour == that.endHour && // 新增比较
                endMinute == that.endMinute && // 新增比较
                Objects.equals(phone, that.phone) &&
                Objects.equals(name, that.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, phone, name, volume, interval, times, enabled,
                startHour, startMinute, endHour, endMinute); // 更新 hash
    }

    @NonNull
    @Override
    public String toString() {
        return "RingerSetting{" +
                "id=" + id +
                ", phone='" + phone + '\'' +
                ", name='" + name + '\'' +
                ", volume=" + volume +
                ", interval=" + interval +
                ", times=" + times +
                ", enabled=" + enabled +
                ", startHour=" + startHour +
                ", endHour=" + endHour +
                ", startMinute=" + startMinute +
                ", endMinute=" + endMinute+"}" ;
    }
}